const Router = require("koa-router");
const Mock = require("mockjs")
const router = new Router({
    prefix: "/api"
});

router.get("/", async function (ctx) {
    ctx.body = "服务器运行正常"
});

// router.options("/users",async function(ctx){
//     ctx.body = "options"
// })

router.post("/users",async function(ctx){
    ctx.t
    ctx.body = "post-添加"
})

router.put("/users",async function(ctx){
    ctx.body = "put-更新"
})

router.delete("/users",async function(ctx){
    ctx.body = "delete-删除"
})

function sleep(){
    return new Promise(function(resolve){
        setTimeout(resolve,10000)
    })
}

router.get("/users", async function (ctx) {
    // await sleep();
    // asdasda
    // ctx.status = 401;
    // ctx.status = 403;
    // ctx.status = 404;
    // ctx.status = 500;
    // ctx.status = 501;
    ctx.body = Mock.mock({
        state: true,
        msg: "成功",
        "users|4": [
            {
                No: "@id",
                name: "@cname",
                "sex|1": ["男", "女", "未知"],
                tel: /^(1[0-9]{10})$/,
                img: "https://api.ixiaowai.cn/mcapi/mcapi.php?_=@datetime(T)"
            }
        ]
    })
});

router.get("/list", async function (ctx) {
    ctx.body = Mock.mock({
        state: true,
        msg: "成功",
        "users|4": [
            {
                No: "@id",
                name: "@cname",
                "sex|1": ["男", "女", "未知"],
                tel: /^(1[0-9]{10})$/,
                img: "https://api.ixiaowai.cn/mcapi/mcapi.php?_=@datetime(T)"
            }
        ]
    })
    // ctx.body = Mock.mock({
    //     state: false,
    //     msg: "未登录,无权访问数据",
    //     users:null
    // })
});

router.get("/query",async function(ctx){
    // koa 服务器接收的URL地址参数
    let query = ctx.query;
    let result = {
        info:"登录失败,用户名或密码错误"
    }
    let state = false;
    if(query.username=="admin"&&query.password=="123456"){
        result.info = "登录成功"
        state = true;
    }
    ctx.body = {
        state,
        msg:"URL参数接收成功",
        query:{
            info:"传递的URL地址参数",
            query
        },
        result
    }
})

router.post("/params",async function(ctx){
    // ctx.request.body // 加载有 koa-body 插件根据请求参数类型解析后的参数
    let result = {
        state:true,
        msg:"请求体参数",
        params:{
            info:"传递的请求报文体参数",
            params:ctx.request.body
        }
    }
    if(ctx.request.files&&ctx.request.files.img){
        result.files={
            info:"传递的请求报文体二进制文件",
            files:ctx.request.files,
            path:{
                img:"http://127.0.0.1:3000/upload/"+ctx.request.files.img.newFilename
            }
        }
    }
    ctx.body = result
})

router.post("/files",async function(ctx){
    // ctx.request.body // 加载有 koa-body 插件根据请求参数类型解析后的参数
    let result = {
        state:true,
    }
    if(ctx.request.files&&ctx.request.files.img){
        result.path={
            img:"http://127.0.0.1:3000/upload/"+ctx.request.files.img.newFilename
        }
    }
    ctx.body = result
})

router.get("/stu",async function(ctx){
    ctx.body = {
        state:true,
        msg:"查询成功",
        resut:"多个学生数据"
    }
})
router.get("/stu/:id",async function(ctx){
    ctx.body = {
        state:true,
        msg:"查询成功",
        resut:"单个学生数据",
        rest:ctx.params
    }
})

router.delete("/stu/:id",async function(ctx){
    ctx.body = {
        state:true,
        msg:"删除成功",
        // ctx.params 拿去路径上的动态地址
        rest:ctx.params
    }
})

// const token = require("jsonwebtoken");
router.get("/login",async function(ctx){
    let user = {
        id:500,
        username:"admin"
    }

    // token.sign(需要加密的数据对象,加密秘钥,token的相关配置-有效期)
    // let tokenStr = token.sign(user,"itant secret",{ expiresIn:"1h" });
    let tokenStr = ctx.token.sign(user,ctx.token.secret,{ expiresIn:"1h" });

    ctx.body = {
        state:true,
        msg:"登录成功",
        data:{
            token:"Bearer "+tokenStr
        }
    }
})

module.exports = router;